package com.android.nearshop.app.activity.main;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.android.nearshop.R;
import com.android.nearshop.app.activity.common.LoadableActivity;
import com.android.nearshop.app.adapter.listadapter.Text3ListAdapter;
import com.android.nearshop.app.adapter.listadapter.Text3ListAdapter.Text3Item;
import com.android.nearshop.app.widget.TitleBar;
import com.android.nearshop.business.cache.DataCacheManager;
import com.android.nearshop.io.protocol.out.Area;
import com.android.nearshop.util.Constant;
import com.android.nearshop.util.NotifyListener;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-16
 * @see : 镇区选择
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class SelCityActivity extends LoadableActivity 
implements OnItemClickListener
{
    public static final int CODE_REQUEST = 0x8802;
    private Text3ListAdapter listAdapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.view_base_listview);
        
        ListView listView = (ListView)findViewById(R.id.listView);
        listView.setDivider(getResources().getDrawable(R.drawable.content_div));
        listAdapter = new Text3ListAdapter(this);
        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(this);
        loadData();
    }
    
    @Override
    protected void setupTitleBar(TitleBar titleBar)
    {
        titleBar.setRightVisible(false);
        titleBar.setTitle(R.string.title_city);
    }
      
    private void loadData()
    {
        mLoadingView.showLoadingView();
        DataCacheManager.getInstance().getAreaList(
                new NotifyListener<List<Area>>()
        {
            @Override
            public void onSucceed(List<Area> result)
            {
                if(result != null)
                {
                    int size = result.size();
                    List<Text3Item> list = new ArrayList<Text3Item>(size);
                    for(int i =0; i < size; i++)
                    {
                        list.add(result.get(i).setOpenVisible(true));
                    }
                    listAdapter.setList(list);
                    mLoadingView.showMainView();
                }else
                {
                    mLoadingView.showLoadingErrView(R.string.no_result,false);
                }
            }
            
            @Override
            public void onError(int resultCode)
            {
                mLoadingView.showLoadingErrView(false);
            }
        });     
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        Area item = (Area)listAdapter.getItem(position);
        onOK(item);
    }
    
    @Override
    public void refreshView()
    {
        loadData();
    }
    
    private void onOK(Area one)
    {
        Intent intent = new Intent();
        intent.putExtra(Constant.TAG_CITY, (Serializable)one);
        setResult(RESULT_OK, intent);
        finish();
    }
    
    public static void startActivityResult(Activity context)
    {        
        context.startActivityForResult(getStartActivity(context), CODE_REQUEST);
    }
    
    public static Intent getStartActivity(Activity context)
    {        
        Intent intent = new Intent(context, SelCityActivity.class);
        return intent;
    }
}
